<template>
  <div class="page-search">
    <wj-form v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <h2 class="header">高级检索</h2>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button icon="el-icon-refresh" @click="handleResetClick">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="handleSearchClick"
            >搜索</el-button
          >
        </div>
      </template>
    </wj-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import WjForm from '@/base-ui/wj-form'

export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object,
      required: true
    }
  },
  components: {
    WjForm
  },
  emits: ['resetBtnClick', 'searchBtnClick'],
  setup(props, { emit }) {
    const formItems = props.searchFormConfig?.formItems ?? []

    // 属性对应
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ''
    }
    const formData = ref(formOriginData)

    // 重置
    const handleResetClick = () => {
      // for (const key of formOriginData) {
      //   formData.value[key] = formOriginData[key]
      // }
      formData.value = formOriginData
      emit('resetBtnClick')
    }

    // 搜索
    const handleSearchClick = () => {
      emit('searchBtnClick', formData.value)
    }

    return { formData, handleResetClick, handleSearchClick }
  }
})
</script>

<style lang="less" scoped>
.header {
  color: red;
}
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
